<template>
	<view>
		<u-navbar :border-bottom='false' title="入驻皖泗方">
		</u-navbar>
		<view class="inputbox" style="margin-top: 120upx;">
			<input :adjust-position='false' v-model="title" placeholder="请输入门诊名称(必填)" type="text">
		</view>
		<view class="inputbox" style="margin-top: 22upx;">
			<input :adjust-position='false' v-model="name" placeholder="请输入真实姓名(必填)" type="text">
		</view>
		<view class="inputbox" style="margin-top: 22upx;">
			<input @keyup="phone=phone.replace(/[, ]/g,'')" :adjust-position='false' v-model="phone" placeholder="请输入手机号(必填)" type="text">
		</view>
		<view class="icons">请上传相关图片(非必填)</view>
		<view class="upload" style="margin-top: 22upx;">
			<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="5" width="100" height="100">
				<view class="img">
					<image src="/static/img/a1.png" mode="widthFix" style="width: 200upx;height: 200upx;"></image>
				</view>
			</u-upload>
		</view>
		<button hover-class="other-button-hover" class="loginbtn" @click="handle">
			立即申请
		</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userid: '',
				title: '',
				name: '',
				phone: '',
				fileList1: []
			}
		},
		onLoad() {
			let userid = this.GetQueryString('userid');
			console.log(userid)
			this.userid = userid;
		},
		methods: {
			GetQueryString(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
				var r = window.location.search.substr(1).match(reg);
				if (r != null)
					return unescape(r[2]);
				return null;
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				uni.showLoading({
					mask: true
				})
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: getApp().globalData.baseUrl + '/api/Pub/uploadImg', // 接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						header: {
							authoriztion: uni.getStorageSync('token')
						},
						success: (res) => {
							setTimeout(() => {
								// console.log(JSON.parse(res.data).data[0])
								let data = JSON.parse(res.data).data[0]
								resolve(data)
								uni.hideLoading()
							}, 1000)
						}
					});
				})
			},
			async handle() {
				if (!this.title) {
					uni.showToast({
						title: '请填写门诊名称',
						duration: 500,
						icon: 'none',
					});
					return
				}
				if (!this.name) {
					uni.showToast({
						title: '请填写真实姓名',
						duration: 500,
						icon: 'none',
					});
					return
				}
				if (!this.phone) {
					uni.showToast({
						title: '请填写手机号',
						duration: 500,
						icon: 'none',
					});
					return
				}
				if (!(/^1[123456789]\d{9}$/.test(this.phone))) {
					uni.showToast({
						title: '手机号码有误',
						duration: 500,
						icon: 'none',
					});
					return
				}
				this.submit();
			},
			// 注册接口
			async submit() {
				let urls = []
				if (this.fileList1.length > 0) {
					this.fileList1.forEach(item => {
						urls.push(item.url)
					})
				}
				let res = await this.$api.apply({
					title: this.title,
					name: this.name,
					phone: this.phone,
					imgs: urls.toString(),
					pdid: this.userid
				});
				if (res.code == 200) {
					uni.showModal({
						title: '温馨提示！',
						content: res.msg,
						showCancel: false, //没有取消按钮的弹框
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
							}
						}
					});
				}
			}
		}
	}
</script>

<style scoped lang="less">
	.loginbtn {
		width: 550upx;
		background: #74BC42;
		box-shadow: 0px 16upx 35upx 0px rgba(98, 164, 52, 0.32);
		border-radius: 45upx;
		font-size: 36upx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		margin-top: 100upx;
	}
	.inputbox {
		width: 660upx;
		height: 94upx;
		display: flex;
		align-items: center;
		margin: 0 auto;
		box-sizing: border-box;
		border-bottom: 2upx solid #EEEEEE;
		input {
			flex: 1;
			background-color: #FFFFFF;
			height: 60upx;
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: 500;
		}
	}
	.icons {
		width: 660upx;
		height: 94upx;
		display: flex;
		align-items: center;
		margin: 0 auto;
		box-sizing: border-box;
		color: #666;
	}
	.upload {
		margin: 24upx 45upx;
		/deep/ .u-upload__wrap__preview {
			&:nth-child(3n) {
				margin-right: 0upx;
			}
		}
	}
</style>